package com.person.jiaiplan.controller;


import com.person.jiaiplan.assembler.ExpensesAssembler;
import com.person.jiaiplan.pojo.R;
import com.person.jiaiplan.pojo.dto.expenses.ExpensesCountListResDTO;
import com.person.jiaiplan.pojo.dto.expenses.ExpensesCountReqDTO;
import com.person.jiaiplan.pojo.dto.expenses.ExpensesCountResDTO;
import com.person.jiaiplan.pojo.dto.expenses.NoPlanExpensesReqDTO;
import com.person.jiaiplan.pojo.entity.ExpensesCountLogs;
import com.person.jiaiplan.service.ExpensesCountLogsService;
import com.person.jiaiplan.service.ExpensesService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 计划统计接口
 */
@AllArgsConstructor
@RestController
@RequestMapping("expenses/count/")
@Validated
@Slf4j
public class ExpensesCountController {


    /**
     * 计划花销
     */
    @Autowired
    private ExpensesService expensesService;

    @Autowired
    private ExpensesCountLogsService expensesCountLogsService;

    /**
     * 查询计划统计
     *
     * @param
     * @return
     */
    @PostMapping("query")
    public R<ExpensesCountResDTO> queryCount(@RequestBody @Validated ExpensesCountReqDTO reqDTO) {
        return R.ok(expensesCountLogsService.queryCount(reqDTO));
    }

    /**
     * 非计划消费
     *
     * @param reqDTO
     * @return
     */
    @PostMapping("noPlanExpenses")
    public R<Boolean> noPlanExpenses(@RequestBody @Validated NoPlanExpensesReqDTO reqDTO) {
        return R.ok(expensesCountLogsService.noPlanExpenses(reqDTO));
    }

    @PostMapping("update")
    public R<Boolean> update(@RequestBody @Validated ExpensesCountListResDTO reqDTO) {
        // 大于5 是非计划消费
        if (reqDTO.getExpensesType() > 5){
            reqDTO.setPlanId("0");
        }
        ExpensesCountLogs expensesCountLogs = ExpensesAssembler.INSTANCE.reqDTO2ExpensesCount(reqDTO);
        return R.ok(expensesCountLogsService.updateById(expensesCountLogs));
    }

    @DeleteMapping("delete")
    public R<Boolean> delete(@RequestParam("id") String id) {
        return R.ok(expensesCountLogsService.removeById(id));
    }

}
